package com.cap.dreamcircle.Model.Impl;

import com.cap.dreamcircle.Api.UserService;
import com.cap.dreamcircle.Model.Bean.AnonymousResponseBean;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.Bean.UserInfoBean;
import com.cap.dreamcircle.Model.IUserModel;
import com.cap.dreamcircle.Utils.MD5Utils;
import com.cap.dreamcircle.Utils.ServiceFactory;
import com.cap.dreamcircle.Utils.TransformUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.RequestBody;
import rx.Observable;

/**
 * 用户相关接口的所有实现
 * Created by liucxu on 2017/7/11.
 */

public class UserModelImpl implements IUserModel {
    /**
     * 创建匿名用户
     * @param params
     * @return
     */
    @Override
    public Observable<AnonymousResponseBean> CreateAnonymousUser(HashMap<String, String> params) {
        params.put("task", "deviceRegister");
        return ServiceFactory.GetInstance().createService(UserService.class).CreateAnonymousUser(params);
    }

    /**
     * 获取注册验证码
     * @param params
     * @return
     */
    @Override
    public Observable<BaseBean> GetVerificationCode(HashMap<String, String> params) {
        params.put("task", "sendVerification");
        params.put("type", "register");
        params.put("countryCode", "+86");
        return ServiceFactory.GetInstance().createService(UserService.class).GetVerificationCode(params);
    }

    /**
     * 获取登录验证码
     * @param params
     * @return
     */
    @Override
    public Observable<BaseBean> GetVerificationCodeLogin(HashMap<String, String> params) {
        params.put("task", "sendVerification");
        params.put("type", "login");
        params.put("countryCode", "+86");
        return ServiceFactory.GetInstance().createService(UserService.class).GetVerificationCode(params);
    }

    /**
     * 手机验证码登录
     * @param params
     * @return
     */
    @Override
    public Observable<AnonymousResponseBean> PhoneLogin(HashMap<String, String> params) {
        params.put("task","login");
        return ServiceFactory.GetInstance().createService(UserService.class).PhoneLogin(params);
    }

    /**
     * 完善用户信息接口
     * @param params
     * @return
     */
    @Override
    public Observable<BaseBean> CompleteUserInfo(Map<String, RequestBody> params) {
        return ServiceFactory.GetInstance().createService(UserService.class).CompleteUserInfo(params);
    }

    @Override
    public Observable<BaseBean> BindUserPhone(HashMap<String, String> params) {
        params.put("task","bindMobile");
        return ServiceFactory.GetInstance().createService(UserService.class).BindUserPhone(params);
    }

    @Override
    public Observable<BaseBean> BindUserPhoneTest(HashMap<String, String> params) {
        params.put("task","bindMobileTest");
        return ServiceFactory.GetInstance().createService(UserService.class).BindUserPhoneTest(params);
    }

    /**
     * 获取绑定验证码
     * @param params
     * @return
     */
    @Override
    public Observable<BaseBean> GetVerificationCodeBind(HashMap<String, String> params) {
        params.put("task", "sendVerification");
        params.put("type", "bind");
        params.put("countryCode", "+86");
        return ServiceFactory.GetInstance().createService(UserService.class).GetVerificationCode(params);
    }

    @Override
    public Observable<AnonymousResponseBean> wechatLogin(Map<String, RequestBody> params) {
//        params.put("wxUnionId",unionId);
//        params.put("verify", MD5Utils.GetMD5Value("sendVerificationDreamSecurityData"+unionId));
        return ServiceFactory.GetInstance().createService(UserService.class).wechatLogin(params);
    }

    @Override
    public Observable<BaseBean> BindWechat(String unionId) {
        HashMap<String,String> params=new HashMap<>();
        params.put("task","bindWx");
        params.put("wxUnionId",unionId);
        return ServiceFactory.GetInstance().createService(UserService.class).BindWechat(params);
    }

    @Override
    public Observable<BaseBean> TestTokenValidate() {
        HashMap<String,String> params=new HashMap<>();
        params.put("task","testToken");
        return ServiceFactory.GetInstance().createService(UserService.class).TestTokenValidate(params);
    }

    @Override
    public Observable<UserInfoBean> RequestUserInfo() {
        HashMap<String,String> params=new HashMap<>();
        params.put("task", "queryUserInfo");
        return ServiceFactory.GetInstance().createService(UserService.class).RequestUserInfo(params);
    }

    @Override
    public Observable<BaseBean> CountKey(String key) {
        HashMap<String,String> params=new HashMap<>();
        params.put("task", "keepalive");
        params.put("eventId", key);
        return ServiceFactory.GetInstance().createService(UserService.class).CountKey(params);
    }

    @Override
    public Observable<BaseBean> TestBindWechat(String wxUinonId) {
        HashMap<String,String> params=new HashMap<>();
        params.put("task", "testBdWx");
        params.put("wxUnionId", wxUinonId);
        return ServiceFactory.GetInstance().createService(UserService.class).TestBindWechat(params).compose(TransformUtils.<BaseBean>defaultSchedulers());
    }

    @Override
    public Observable<BaseBean> DoBindWechat(Map<String, RequestBody> params) {
        return ServiceFactory.GetInstance().createService(UserService.class).DoBindWechat(params).compose(TransformUtils.<BaseBean>defaultSchedulers());
    }

    @Override
    public Observable<BaseBean> BindVerificationCode(String mobile) {
        HashMap<String,String> params=new HashMap<>();
        params.put("task", "sendBdVerification");
        params.put("mobile", mobile);
        params.put("countryCode","+86");
        params.put("verify", MD5Utils.GetMD5Value("sendVerificationDreamSecurityData"+mobile));
        return ServiceFactory.GetInstance().createService(UserService.class).BindVerificationCode(params).compose(TransformUtils.<BaseBean>defaultSchedulers());
    }

    @Override
    public Observable<BaseBean> BindPhoneWithCode(String mobile, String verification) {
        HashMap<String,String> params=new HashMap<>();
        params.put("task", "bdMobile");
        params.put("mobile", mobile);
        params.put("verification",verification);
        return ServiceFactory.GetInstance().createService(UserService.class).BindPhoneWithCode(params).compose(TransformUtils.<BaseBean>defaultSchedulers());
    }

    /**
     * 手机验证码注册
     * @param params
     * @return
     */
    @Override
    public Observable<AnonymousResponseBean> CreatePhoneUserAccount(HashMap<String, String> params) {
        params.put("task", "mobileRegister");
        return ServiceFactory.GetInstance().createService(UserService.class).CreatePhoneUserAccount(params);
    }
}
